const app = getApp();
Page({
      data: {},
      onLoad: function (options) {
            this.drawImage();
      },
      drawImage() {

            let that = this;
            let context = wx.createCanvasContext('myCanvas');
            let mobileScreen = app.globalData.screen;
            let userInfo = {
                  advantage: "英语专业八级证书",
                  age: 29,
                  categoryId: 135,
                  categoryName: "英语口语",
                  courseDesc: "Mr题题题而牛股鹅绒修护",
                  createTime: "2021-01-20 02:19:22",
                  headImg: "https://eco-culture.oss-cn-shenzhen.aliyuncs.com/images/bear/t2.jpg",
                  id: 54,
                  introduce: "在工作中细致认真，乐于接受挑战；有良好的沟通能力，注重团队合作，在积极听取他人意见的同时也会提出自己的见解。英语口语能力突出，英国剑桥大学毕业。",
                  limitCount: 0,
                  mobile: null,
                  name: "刘莹",
                  nickName: null,
                  orderNum: null,
                  orderPrice: 32000,
                  orderTime: "9,10,11,12,13,14,15,16,17,18,19,20,21",
                  other: "雅思口语8分",
                  parentId: 116,
                  parentName: "语种",
                  postgraduate: "国内在读研究生",
                  receivingType: 1,
                  settingList: [],
                  sort: 10,
                  status: 1,
                  teachAge: 7,
                  undergraduate: "2010-2014英国剑桥大学",
                  updateTime: "2021-01-26 11:06:48",
                  userId: 39,
                  weekTime: "1,2,3,4,5,6,7",
                  working: "中国国旅文案策划，陪同翻译",
                  yeCategoryId: 99,
                  yeCategoryName: "文化类",
            }
            // access_token: "ddffd3b3-46f6-4afd-902b-806d0e9b9bbd"
            // enabled: true
            // expires_in: 43199
            // nickName: "Rainie。"
            // pic: "http://eco-culture.oss-cn-shenzhen.aliyuncs.com/2021/01/972645f692ab429182f66e934a495918.ico"
            // refresh_token: "7047f12f-2ebb-411a-ba63-da5d5fe58cf2"
            // token_type: "bearer"
            // userId: 56
            context.drawImage('../../images/canvasBg.png', 0, 0, mobileScreen.w,);


            console.log(userInfo, 'userInfo==')
            context.font = 'bold 14px 微软雅黑'
            // context.setFontSize(20)
            context.setFillStyle("#EB785D")
            context.fillText('姓名:', 30, 80);

            context.setFillStyle("#EB785D")
            context.fillText('年龄:', 30, 110);

            context.setFillStyle("#EB785D")
            context.fillText('教龄:', 30, 140);

            context.setFillStyle("#EB785D")
            context.fillText('个人优势:', 30, 170);





            context.font = 'bold 12px 微软雅黑'
            let nickName = userInfo.name
            // context.setFontSize(16)
            context.setFillStyle("#fff")
            context.fillText(nickName, 80, 80);

            context.setFillStyle("#fff")
            context.fillText(userInfo.age, 80, 110);

            context.setFillStyle("#fff")
            context.fillText(userInfo.teachAge, 80, 140);

            context.setFillStyle("#fff")
            context.fillText(userInfo.introduce, 100, 170);
            context.draw()
            // this.downloadImage().then(res => {
            //       // 图片高/宽=1.6
            //       let context = wx.createCanvasContext('myCanvas');
            //
            //       let h = app.globalData.screen.h, w = app.globalData.screen.w;
            //       //垂直中心点
            //       let centerh = canvasHeight / 2;
            //       //水平中心点
            //       let centerW = w / 2;
            //
            //       context.fillRect(h, w, centerh, 0);
            //       // 设置背景图片宽度为屏幕的0.75
            //       let bgw = app.globalData.screen.w * 0.75
            //       // 绘制背景图片   centerW-(bgw/2)画布中心点减去一个图片宽度的一半居中
            //       context.drawImage('../../img/ercode.png', centerW - (bgw / 2), centerh - 200, bgw, 400);
            //
            //       that.setData({
            //             BtnPos: {
            //                   x: (bgw + centerW - (bgw / 2) + 375 - app.globalData.screen.w) * 2,
            //                   y: centerh - 200
            //             }
            //       })
            //       console.log(that.data.BtnPos.x, 'x----')
            //       // context.drawImage('../../img/basicprofile.jpeg', centerW -30, 50, 60, 60);
            //
            //       // 绘制圆角头像
            //       circleImg(context, res, centerW - 120, centerh - 180, 20);
            //       context.setFillStyle("#000")
            //
            //       context.setFontSize(14)
            //       let nickName = that.data.member.nickname.substring(0, 10)
            //       context.fillText(nickName, centerW - 60, centerh - 170);
            //       // 邀请码背景框
            //       let str = that.data.member.iv_no.toString().length
            //       let textW = (Number(str) + 4) * 10
            //       // 以每个字体的10rpx计算长度+邀请码的长度
            //       context.setFillStyle('#f3d481')
            //       context.fillRect(centerW - 60, centerh - 155, textW, 20)
            //
            //
            //       context.setFontSize(11)
            //       context.setFillStyle('#fff')
            //       context.fillText("邀请码:" + that.data.member.iv_no, centerW - 50, centerh - 140)
            //
            //       context.setFillStyle("#999")
            //       context.setFontSize(12)
            //       context.fillText("邀请您一起共享品质生活", centerW - 25, centerh - 110)
            //       // context.fillText('Hello', 20, 20)
            //
            //       // context.drawImage('../../img/code.jpg', w / 2 + 125, 200, 60, 60);
            //       // 绘制用户名
            //
            //       // context.draw();
            // })


      },
      circleImg(ctx, img, x, y, r) {
            ctx.save();
            let d = 2 * r;
            let cx = x + r;
            let cy = y + r;
            console.log("2222")
            // ctx.arc(cx, cy, r, 0, 2 * Math.PI);
            ctx.arc(cx, cy, r, 0, Math.PI * 2, false)
            ctx.fill();
            ctx.clip();
            console.log(img, '9mg====')
            ctx.drawImage(img, x, y, d, d);
            ctx.restore();
      },
      closeImage() {
            console.log("2222")
            this.setData({
                  showShareImage: false
            })
      },
      downloadImage() {
            let that = this;
            return new Promise((re, rj) => {
                  wx.downloadFile({
                        url: app.globalData.userServer.headimg,
                        success: function (res) {
                              console.log(res.tempFilePath);

                              re(res.tempFilePath)

                        }, fail: function (fres) {
                              console.log(fres, 'res----')
                        }
                  })
            })

      },
      saveImageMethods() {
            let that = this;
            wx.canvasToTempFilePath({     //将canvas生成图片
                  canvasId: 'myCanvas',
                  // x: 0,
                  // y: 0,
                  // width: this.data.imageData.imageWidth+50,
                  // height: this.data.imageData.imageHeight,
                  // destWidth: app.globalData.screen.w,     //截取canvas的宽度
                  // destHeight:  app.globalData.screen.h,   //截取canvas的高度
                  success: function (res) {
                        wx.saveImageToPhotosAlbum({  //保存图片到相册
                              filePath: res.tempFilePath,
                              success: function () {

                                    wx.showToast({
                                          title: "生成图片成功！",
                                          duration: 2000
                                    })
                              }
                        })
                  },
                  fail: (error) => {
                        console.log(error, '保存相片出错')
                  }
            })
      },
      saveImageToMobile() {
            let that = this;
            wx.getSetting({
                  success(res) {
                        console.log("getSetting: success");
                        if (!res.authSetting['scope.writePhotosAlbum']) {
                              // 接口调用询问
                              wx.authorize({
                                    scope: 'scope.writePhotosAlbum',
                                    success() {
                                          that.saveImageMethods();
                                    },
                                    fail() {
                                          // 用户拒绝了授权
                                          wx.showModal({
                                                title: '保存图片',
                                                content: '保存图片需要您授权',
                                                showCancel: true,
                                                confirmText: '确定',

                                                success: function (res) {
                                                      if (res.confirm) {
                                                            // 打开设置页面
                                                            wx.openSetting({
                                                                  success: function (data) {
                                                                        if (data.authSetting['scope.writePhotosAlbum']) {
                                                                              that.saveImageMethods();
                                                                        } else {
                                                                              console.log("授权失败");
                                                                        }
                                                                  },
                                                                  fail: function (data) {
                                                                        console.log("openSetting: fail");
                                                                  }
                                                            });
                                                      } else if (res.cancel) {
                                                            console.log('用户点击取消')
                                                      }

                                                }
                                          })


                                    }
                              })
                        } else {
                              that.saveImageMethods()
                        }
                  },
                  fail(res) {
                        console.log("getSetting: fail");
                        console.log(res);
                  }
            })
      },
});